package com.sinodata.bsm.cicp.finder.was6;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import com.sinodata.bsm.cicp.finder.Finder;
import com.sinodata.bsm.cicp.util.CollectorUtil;

public class WebAppFinder extends Finder {

    private static final Logger logger = Logger.getLogger(WebAppFinder.class);

    /* public List find(ResValue resValue, ResAttributeValValue[] attrVals, ResAttributeValue[] attributes) {
         List retList = new ArrayList();
         try {
             String url = Util.getWasPerfToolUrl(host, attrVals[0].getAttrValue());
             String respXml = CollectorUtil.getHttpXml(url + "?Module=webAppModule");
             Document doc = DocumentHelper.parseText(respXml);
             Element root = doc.getRootElement();
             List<Element> nodes = root.elements("Node");
             for (Element node : nodes) {
                 String nodeName = node.attributeValue("name");
                 if (nodeName.equals(attrVals[2].getAttrValue())) {
                     List<Element> servers = node.elements("Server");
                     for (Element server : servers) {
                         String serName = server.attributeValue("name");
                         List<Element> data = server.selectNodes("Stat/Stat");
                         for (Element cp : data) {
                             if (serName.equals(attrVals[1].getAttrValue())) {
                                 String webAppName = cp.attributeValue("name");
                                 retList.add(creatRes(serName, nodeName, webAppName, attributes));
                             }
                         }
                     }
                 }
             }
         } catch (Exception e) {
             logger.error("failed to find was6 webapp" + e);
         }
         return retList;
     }*/

    /*private List creatRes(String serName, String nodeName, String webAppName, ResAttributeValue[] attributes) {
        List aRes = new ArrayList();
        ResValue resValue = new ResValue();
        resValue.setIp(host);
        resValue.setName(webAppName);
        //去掉应用名称中的特殊符号#
        String appName = webAppName.replace("#", " ");
        resValue.setInternalId(appName);
        aRes.add(resValue);
        aRes.add(com.union.pub.util.CollectorUtil.getResAttrValValue(attributes[0].getId(), serName));
        aRes.add(com.union.pub.util.CollectorUtil.getResAttrValValue(attributes[1].getId(), nodeName));
        aRes.add(com.union.pub.util.CollectorUtil.getResAttrValValue(attributes[2].getId(), webAppName));
        return aRes;
    }*/

    @SuppressWarnings("unchecked")
    @Override
    public List<String[]> find(String ip, Map params) {
        List<String[]> list = new ArrayList<String[]>();
        try {
            String port = (String) params.get("SERVERPORT");
            String url = Util.getWasPerfToolUrl(ip, port);
            String urlStr = url + "?Module=webAppModule";
            String respXml = CollectorUtil.getJmxXml(urlStr, ip, Integer.parseInt(port));
            Document doc = DocumentHelper.parseText(respXml);
            Element root = doc.getRootElement();
            List<Element> nodes = root.elements("Node");
            for (Element node : nodes) {
                String nodeName = node.attributeValue("name");
                if (nodeName.equals(params.get("NODENAME"))) {
                    List<Element> servers = node.elements("Server");
                    for (Element server : servers) {
                        String serName = server.attributeValue("name");
                        List<Element> data = server.selectNodes("Stat/Stat");
                        for (Element cp : data) {
                            if (serName.equals(params.get("SERVERNAME"))) {
                                String webAppName = cp.attributeValue("name");
                                //去掉应用名称中的特殊符号#
                                String appName = webAppName.replace("#", " ");
                                String[] info = new String[6];
                                list.add(info);
                                info[0] = appName;
                                info[1] = webAppName;
                                info[2] = webAppName;
                                info[3] = serName;
                                info[4] = nodeName;
                                info[5] = webAppName;
                            }
                        }
                    }
                }
            }
            return list;
        } catch (Exception e) {
            logger.error("failed to find was6 webapp" + e);
            return new ArrayList<String[]>();
        }
    }
}
